package main;

import gate.IGateEx;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


public class GateComm extends Thread
{
	private BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
	private static GateComm instance;
	private IGateEx gate;
	public synchronized void addTask(Runnable task)
	{
		System.out.println("Adding Task");
		taskQueue.add(task);
	}
	@Override
	public void run()
	{
		while(true)
		{
			Runnable job = null;
			try
			{
				job = taskQueue.take();
				System.out.println("Doing task");
				if(job == null)
				{
					System.out.println(" job null ? ");
				}
				job.run();
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
		}
	}
	public static void setUpGateComm(IGateEx gate)
	{
		if(instance != null)
		{
			return;
		}
		instance = new GateComm(gate);
		GateTaskFactory.setUpFactory(gate);
		instance.start();
	}
	private GateComm(IGateEx gate)
	{
		this.gate = gate;
		setDaemon(false);
	}
	public static GateComm getComm() { return instance; }
}
